//
//  InPhoneViewController.m
//  Inner  Voice
//
//  Created by brother on 16/3/17.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InPhoneViewController.h"

#import "InZhuCeViewController.h"

#import <SMS_SDK/SMSSDK.h>

#import "AppDelegate.h"

@interface InPhoneViewController ()
//手机号
@property (weak, nonatomic) IBOutlet UITextField *mobliephoneFieldText;
//验证码
@property (weak, nonatomic) IBOutlet UITextField *verificationFieldText;
//获取验证码
@property (weak, nonatomic) IBOutlet UIButton *gettBtn;
//下一步
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
//定时器60s
@property (nonatomic, assign)NSInteger number;

@property (nonatomic, strong)NSTimer *timer;

@property (nonatomic, copy)NSString *currentTel;

@end

@implementation InPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [SMSSDK registerApp:@"fef4cf5e78cb" withSecret:@"bfed04940cecf2dfe771a10ab38fdc48"];
    // Do any additional setup after loading the view from its nib.
}
//封装UIAlertView方法，直接调用就可以显示提示
-(void)showAlertView:(NSString *)string {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:string delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
}

//获取验证码
- (IBAction)gettBtnClick:(id)sender {
    if ([[AppDelegate shareAppdelegate] canConnectedToTheInternet]) {
    if (self.mobliephoneFieldText.text.length == 11) {
//        发送手机验证码
        [self offTimer];
        self.gettBtn.selected = YES;
        self.gettBtn.userInteractionEnabled = NO;
        self.number = 60;
        [self onTimer];
        [self sendTelNumber];
    }else {
        [self showAlertView:@"请输入11位有效号码"];
    }
    }else {
        [self showAlertView:@"当前网络不可用"];
    }
}
- (void)sendTelNumber {
    self.currentTel = nil;
    self.currentTel = self.mobliephoneFieldText.text;
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.currentTel zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (!error) {
            NSLog(@"获取验证码");
        }else {
            NSLog(@"错误消息:%@",error);
        }
    }];
}
//下一步
- (IBAction)nextBtnClick:(id)sender {
#warning 验证码  打开！！！！！！！！！
    
//    //判断是否有输入验证码
//    if (self.mobliephoneFieldText.text.length == 0) {
//        return;
//    }else if (self.verificationFieldText.text.length == 0) {
//        return;
//    }else {
////        如果验证码正确  跳转编辑页面
//        [SMSSDK commitVerificationCode:self.verificationFieldText.text phoneNumber:self.currentTel zone:@"86" result:^(NSError *error) {
//            if (!error) {
//                NSLog(@" 验证成功");
//                把手机号传给下一个控制器
                InZhuCeViewController *contro = [[InZhuCeViewController alloc]initWithTel:self.mobliephoneFieldText.text];
                
                [self presentViewController:contro animated:YES completion:nil];
//            }else {
//                [self showAlertView:@"验证码无效"];
//            }
//        }];
//    }
}
//关闭定时器
- (void)offTimer {
    self.number = 0;
    self.gettBtn.selected = NO;
    self.gettBtn.userInteractionEnabled = YES;
    [self.gettBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.timer invalidate];
    self.timer = nil;
}
//启用计时器
- (void)onTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(setCodeBtnTitle) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];
    [self.timer fire];
}
//按钮倒计时
- (void)setCodeBtnTitle {
    if (self.number > 0) {
        self.number--;
        self.gettBtn.titleLabel.text = [NSString stringWithFormat:@"已发送短信(%02ld)",(long)self.number];
        [self.gettBtn setTitle:[NSString stringWithFormat:@"已发送短信(%02ld)",(long)self.number] forState:UIControlStateSelected];
    } else {
        [self offTimer];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//销毁定时器
- (void)dealloc {
    
    [self offTimer];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
